package com.example.BluetoothTest;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import java.lang.String;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.io.IOException;

import java.util.Timer;

public class BtInterfaceDisplay implements CommandListener{
	
    private BluetoothTestMidlet parent;
    private BTDeviceDiscovery btDiscovery;
    private Command exit;
    private Image onImage;
    private Timer BtRefresh = null;
    private int refresh_interval = 120000;
    public List SearchedDevList;
   // private String BtDeviceAddr;
    public java.lang.String BtAddr;
    Display display;
    BtInterfaceDisplay(BluetoothTestMidlet parent)
    {
    	this.parent = parent;
    	btDiscovery = new BTDeviceDiscovery(this);
    	BtRefresh = new Timer();
    	BtRefresh.schedule(btDiscovery, 0, refresh_interval);

    	exit = new Command("Exit", Command.EXIT, 2);
    	SearchedDevList = new List("Searching Devices...", List.IMPLICIT);
    	SearchedDevList.addCommand(exit);
        try {
            onImage = Image.createImage("../images/Phone.png");
        } catch (IOException e) {
            // provide off-screen image then
            onImage = createIndicatorImage(12, 12, 0, 255, 0);
        }
        //SearchedDevList.set(0, SearchedDevList.getString(0), onImage);
    	SearchedDevList.append("Searched Devices", onImage);
    	SearchedDevList.setCommandListener(this);
    	
    	//SearchedDevList.append("Searching...", null);
    	display = Display.getDisplay(parent);
    	display.setCurrent(SearchedDevList);
    	//FindDevices();
    }
    
    public void commandAction(Command c, Displayable d) {
    	if (c == exit){                                              //exit triggered from the main form
    		destroy();
            parent.show();  
        }
    	
    }
    
    /** Destroys this component. */
    void destroy() {
        // finalize the image server work
    	SearchedDevList.deleteAll();
    }
    
    void updateDeviceList(String BtDeviceAddr)
    {
    	this.do_alert("updateDeviceList...", 400);
    	//this.do_alert(BtAddr, 400);
    	SearchedDevList.setTitle("Searched Device");
    	SearchedDevList.append(BtDeviceAddr, null);
    	display = Display.getDisplay(parent);
    	display.setCurrent(SearchedDevList);
    }
    
    private Image createIndicatorImage(int w, int h, int r, int g, int b) {
        Image res = Image.createImage(w, h);
        Graphics gc = res.getGraphics();
        gc.setColor(r, g, b);
        gc.fillRect(0, 0, w, h);

        return res;
    }
   /* public void getBtAddr(String BtAddr)
    {
    	BtDeviceAddr = BtAddr;
    }*/
    void do_alert(String msg,int time_out){
        if (display.getCurrent() instanceof Alert ){
            ((Alert)display.getCurrent()).setString(msg);
            ((Alert)display.getCurrent()).setTimeout(time_out);
        }else{
            Alert alert = new Alert("Bluetooth");
            alert.setString(msg);
            alert.setTimeout(time_out);
            display.setCurrent(alert);
        }
    }
}
